
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>


using std::cout;
using std::cin;
using std::endl;

using std::rand;
using std::srand;


void printGameStatus( const std::string userGuess, int guesses )
{
    cout << endl;
    if( guesses != 7 )
        cout << "Guess the word: " << userGuess << endl;

    switch( guesses )
    {
        case 7:
            cout << " o" << endl;
        case 6:
            if( guesses == 6 )
                cout << endl;
            cout << "/";
        case 5:
            if( guesses == 5 )
                cout << endl << " |";
            else
                cout << "|";
        case 4:
            if( guesses == 4 )
                cout << endl << "  \\" << endl;
            else
                cout << "\\" << endl;
        case 3:
            if( guesses == 3 )
                cout << endl << endl;
            cout << " |" << endl;
        case 2:
            if( guesses == 2 ) 
                cout << endl << endl << endl;
            cout << "/";
        case 1:
            if( guesses == 1 )
                cout << endl << endl << endl << "  \\" << endl;
            else
                cout << " \\" << endl;
            break;
        default:
                cout << endl << endl << endl << endl;
            break;
    }
}

int selectWord( int vectorSize )
{
    return rand() % ( vectorSize - 1 );
}

bool hasLetter( std::string secret, std::string* userGuess, const char c )
{
    int pos;
    bool hasLetter = false;

    pos = secret.find( c );
    while( pos != std::string::npos )
    {
        if( secret.at(pos) != 'X' )
        {
            userGuess->replace( pos, 1, &c );
            secret.replace( pos, 1, "X" );
            hasLetter = true;
        }
        pos = secret.find( c );
    }

    return hasLetter;
}

int main()
{
    std::vector<std::string> words;
    words.push_back( "construir" ); words.push_back( "arruamento" );
    words.push_back( "aeroporto" ); words.push_back( "moldura" );
    words.push_back( "instruir" ); words.push_back( "inconstitucional" );
    words.push_back( "elo" ); words.push_back( "hiena" );
    words.push_back( "rosnar" ); words.push_back( "peixe" );
    words.push_back( "gol" ); words.push_back( "estrada" );
    words.push_back( "cal" ); words.push_back( "interessante" );
    words.push_back( "programar" ); words.push_back( "computador" );
    words.push_back( "mesa" ); words.push_back( "cadeira" );
    words.push_back( "estojo" ); words.push_back( "livro" );
    words.push_back( "extintor" ); words.push_back( "carruagem" );
    words.push_back( "lua" ); words.push_back( "pote" );
    words.push_back( "caixa" ); words.push_back( "bote" );
    words.push_back( "teto" ); words.push_back( "aproveitamento" );
    words.push_back( "asfalto" ); words.push_back( "holanda" );
    words.push_back( "inglaterra" ); words.push_back( "planeta" );
    words.push_back( "alemanha" ); words.push_back( "portugal" );
    words.push_back( "brasil" ); words.push_back( "cano" );
    words.push_back( "quibe" ); words.push_back( "galinha" );
    words.push_back( "ciencia" ); words.push_back( "boto" );

    // Sorteia
    srand( time( 0 ) );

    bool playAgain = true;

    while( playAgain )
    {
        // Descobre a palavra
        std::string secret = words.at( selectWord( words.size() ) );
        std::string userguess( secret.size(), 'X');

        // Prepara jogo pra iniciar
        char letter;
        int numGuesses = 0;

        do
        {
            // Imprime o status atual do jogo
            printGameStatus( userguess, numGuesses );
            
            // le do tecladoa letra que o usuario escolheu
            cin >> letter;

            // Procura pela letra
            if( !hasLetter( secret, &userguess, letter ) )
                numGuesses++;

            // Imprime boneco
            printGameStatus( userguess, numGuesses );
        }
        while( userguess != secret && numGuesses < 7 );

        std::string answer;
        if( numGuesses < 7 )
        {
            printGameStatus( userguess, numGuesses );
            cout << endl << "Congratulations!!! You guessed my word. Play again? yes/no:\n";
            cin >> answer;
        }
        else
        {
            cout << endl << "You loose! Play again? yes/no:\n";
            cin >> answer;
        }

        // Check if should play again
        if( answer == "yes" )
            playAgain = true;
        else
            playAgain = false;
    }
    return 0;
}

